package com.msb.client;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.msb.client.bean.Person;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunpeng
 * @Date 2021-07-19 10:16
 */
public class Demo {
    @Test
    public void demo1() throws Exception{
        //浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //url
        URIBuilder uriBuilder = new URIBuilder("http://127.0.0.1:8080/demoGet");
        //参数
        uriBuilder.addParameter("string", "abc");
        //执行方法
        HttpGet get = new HttpGet(uriBuilder.build());
        //执行
        CloseableHttpResponse response = httpClient.execute(get);
        //响应体
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println(result);
    }

    @Test
    public void demoPost() throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //url
        URIBuilder uriBuilder = new URIBuilder("http://127.0.0.1:8080/demoPost");
        //发法
        HttpPost httpPost = new HttpPost(uriBuilder.build());
        //参数：请求体
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("name", "sunpeng"));
        params.add(new BasicNameValuePair("id", "1"));
        HttpEntity httpEntity = new UrlEncodedFormEntity(params, "utf-8");
        httpPost.setEntity(httpEntity);
        //执行
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //响应体
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println(result);
        //json
        ObjectMapper mapper = new ObjectMapper();
        Person person = mapper.readValue(result, Person.class);
        System.out.println(person);
    }

    @Test
    public void demoJson() throws  Exception{

        //浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //url、方法
        HttpPost post = new HttpPost("http://localhost:8080/json");
        //请求体
        Person person = new Person(12L, "jia");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(person);
        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        post.setEntity(entity);
        //执行
        CloseableHttpResponse response = httpClient.execute(post);
        //响应
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
        //关闭连接
        response.close();
        httpClient.close();

    }
}
